package com.summer.safe.utils;

import com.summer.safe.base.Principal;
import com.summer.safe.config.shiro.RealmConfig;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;

/**
 * Shiro工具
 */
public class ShiroUtils {

    /**
     * 获取当前登录者对象
     */
	public static Principal getPrincipal(){
		try{
			Subject subject = SecurityUtils.getSubject();
			Principal principal = (Principal)subject.getPrincipal();
			if (principal != null){
				return principal;
			}
		}catch (UnavailableSecurityManagerException e) {

		}catch (InvalidSessionException e){

		}
		return null;
	}

	/**
	 * 清除授权的缓存
	 */
	public static void clearCachedAuthorizationInfo() {
		RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
		RealmConfig realm = (RealmConfig) rsm.getRealms().iterator().next();

		realm.clearCachedAuthorizationInfo();
	}

	/**
	 * 退出
	 */
	public static void logout(){
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
	}



}
